package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"import-master/chone"
	"import-master/modler"
	"import-master/pkg"
	"strconv"
	"time"
)

func UserLogin(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	redis_key := "UserLogin" + username
	errCount, _ := chone.Rdb.Get(chone.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user modler.User
	err := modler.DB.Where("username=?", username).First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户查询失败",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号和密码错误",
		})
		return
	}
	if user.Password != pkg.Md5(password) {
		chone.Rdb.Incr(chone.Ctx, redis_key)
		if errCount == 2 {
			chone.Rdb.Set(chone.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "密码错误3次,封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}

func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	data := modler.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := modler.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失功",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	data := modler.RoleNode{

		Rid: rid,
		Nid: nid,
	}
	modler.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func UserRoleList(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		WHERE
		  user_roles.uid = 1
	*/
	var list []modler.Role
	modler.DB.Table("roles").
		Joins(" LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid=?", uid).Debug().Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
}

func RoleNodeList(c *gin.Context) {
	rid := c.Query("rid")
	/*
			  SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes on role_nodes.nid = nodes.id
		WHERE
		  role_nodes.id = 1

	*/
	var list []modler.Node
	modler.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes on role_nodes.nid = nodes.id").
		Where(" role_nodes.rid=?", rid).Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func UserDetail(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var count int64
	modler.DB.Model(&modler.User{}).Count(&count)
	var user []modler.User

	redis_key := "UserDetail" + strconv.Itoa(page)
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.Offset(offset).Limit(size).Find(&user)
		marshal, _ := json.Marshal(user)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  user,
		"count": count,
	})
	return
}

func DetailList(c *gin.Context) {
	id := c.Query("id")
	var user modler.User

	redis_key := "DetailList" + id
	result, _ := chone.Rdb.Get(chone.Ctx, redis_key).Result()
	if result == "" {
		modler.DB.First(&user)
		marshal, _ := json.Marshal(user)
		chone.Rdb.Set(chone.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(result), &user)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": user,
	})
	return
}
